home *** CD-ROM | disk | FTP | other *** search
/ Network Supervisor's Toolkit / Network Supervisor's Toolkit.iso / tools / lu62 / buffmng.c < prev    next >
C/C++ Source or Header  |  1996-07-10  |  5KB  |  145 lines

  1. /*
  2.  Function: The Buffmng subroutine makes the buffer management.
  3.            The following actions take the place while processing the
  4.            "send_data" request:
  5.            - new buffer is allocated;
  6.            - an incoming date is placed in the buffer;
  7.            - the buffer inserted in a resource queue (the next
  8.              buffer processing and deleting it from the queue
  9.              will make by the HS).
  10.            The following actions take the place while processing the
  11.            "receive" request:
  12.            - the HS places the buffer in an input resource queue;
  13.            - the Buffmng copies date into the field which was defined
  14.              in the "receive" request, then
  15.            - deletes the buffer from the queue and frees the buffer memory.
  16.  
  17.  Input: A function code, a pointer to a buffer, a pointer to a buffer pool,
  18.         a pointer to the RCB block, length of a text, a segment descriptor
  19.         (0 - the last segment, 1 - the middle segment).
  20.         The function code values: A - add, D - delete.
  21.         if (code == A) --> pb -> text for filling
  22.         if (code == D) --> pb -> buffer for deleting
  23.                                  from the buff. pool
  24.  Output: the return codes are: "0" - all O.K!
  25.                                "-1" - error...
  26.  
  27.   CopyRight 1995. Nicholas Poljakov all rights reserved.
  28.  
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <malloc.h>
  33. #include <prefix.h>
  34. #include <rcb.h>
  35. #include <state1.h>
  36. #include <string.h>
  37.  
  38. #define ADD 'A'
  39. #define DELETE 'D'
  40.  
  41. buffmng(code, b_ptr, buff, r_ptr, length, dsc, type)
  42. char code;           /* Request code; 'A' - add, 'D' - delete*/
  43. char *b_ptr;         /* Pointer to text buffer  */
  44. char **buff;         /* Field whose contained pointer to buff. chain */
  45. struct rcb *r_ptr;   /* Pointer to RCB */
  46. unsigned int length; /* Length of text */
  47. unsigned char dsc;   /* Segment descriptor; 0 - Last, 1 - middle */
  48. unsigned type;       /* DFC command code */
  49. {
  50.     struct prefix *prf;
  51.     struct prefix *prf1;
  52.     struct prefix *prf2;
  53.     struct tp {
  54.                  unsigned char h;
  55.                  unsigned char l;
  56.               } *t;
  57.     void *prf_sv;
  58.     char *p;
  59.  
  60. #if OS_TYPE == 1
  61. /*********  Trace facility **********/
  62. unsigned int rtype;   /* type of record */
  63. unsigned int pnum;    /* point number */
  64. char pname[8];        /* name of module */
  65. char *drec;       /* record for dump */
  66. int  lenr;            /* record length */
  67.  
  68. rtype = INPROC;
  69. strcpy(pname, "buffmng");
  70. pnum = 1;
  71. drec = &code;
  72. lenr = 1;
  73. gtf(rtype, pname, pnum, drec, lenr);
  74. /***********************************/
  75. #endif
  76.  
  77.     t = (struct tp *)&type;
  78.     switch (code) {
  79.         case ADD :
  80.                   {
  81.                     prf = malloc(sizeof(struct prefix));
  82.                     prf->next = NULL;
  83.                     prf->text = b_ptr;
  84.                     prf -> prev = NULL;
  85.                     prf -> lt_text = length;
  86.                     prf -> dsc = dsc;
  87.             prf -> type = t -> h;
  88.                     if (buff == NULL) {
  89.                        prf1 = r_ptr -> first_out;
  90.                     }
  91.                     else
  92.                             {
  93.                 prf1 = (struct prefix *)*buff;
  94.                             }
  95.                     if (prf1 == NULL) {
  96.                         if (buff == NULL) {
  97.                             r_ptr -> first_out = prf;
  98.                         }
  99.                         else
  100.                 *buff = (char *)prf;
  101.                     }
  102.                             else {
  103.                                     while (prf1->next != NULL) {
  104.                                         prf1 = prf1->next;
  105.                                         }
  106.                                     prf1->next = prf;
  107.                                     prf->prev = prf1; /* back pointer */
  108.                                  }
  109.                     break;
  110.                   }
  111.         case DELETE :
  112.                   {
  113.             prf = (struct prefix *)b_ptr;
  114.                     if ((p = prf->text) != NULL) {
  115.                          free(p);
  116.                     }
  117.                     if (prf->next == NULL) {
  118.                                 if (prf->prev == NULL) {
  119.                                             *buff = NULL;
  120.                                 }
  121.                                 else  {
  122.                                             prf_sv = prf;
  123.                                             prf = prf->prev;
  124.                                             prf->next = NULL;
  125.                                             prf = prf_sv;
  126.                                 }
  127.                     }
  128.                     else
  129.                             {
  130.                                 prf2 = prf->next;
  131.                                 if ((prf1 = prf->prev) != NULL) {
  132.                                      prf1->next = prf2;
  133.                                      prf2->prev = prf1;
  134.                                 }
  135.                                 else {
  136.                        *buff = (char *)prf2;
  137.                                         prf2 -> prev = NULL;
  138.                                 }
  139.                             }
  140.                     free(prf);
  141.                   }
  142.                   }
  143.     return 0;
  144. }
  145.